import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ResData, Status } from 'src/response/res-data.interface';
import { Repository } from 'typeorm';
import { CreateCustomFormDto } from './dto/create-custom-form.dto';
import { UpdateCustomFormDto } from './dto/update-custom-form.dto';
import { CustomForm } from './entities/custom-form.entity';


@Injectable()
export class CustomFormService {

  constructor(@InjectRepository(CustomForm) private customFormRepository: Repository<CustomForm>) { }

  async create(createCustomFormDto: CreateCustomFormDto) {
    let resdata: ResData;
    try {
      const result = await this.customFormRepository.save(createCustomFormDto);
      resdata = {
        code: Status.SUCCESS,
        msg: '保存成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async findAll(querys) {
    let resdata: ResData;
    try {
      const { startTime, endTime, formName } = querys;
      let sql = this.customFormRepository.createQueryBuilder("customForm");
      if (formName) {
        sql.where('customForm.formName like :formName', { formName: `%${querys.formName}%` });
      }
      if (startTime) {
        sql.andWhere('customForm.createdDate between :startTime and :endTime', { startTime, endTime })
      }
      const result = await sql.skip(querys.skip).take(querys.take).orderBy("updatedDate", "DESC").getManyAndCount()

      const [list, total] = result
      resdata = {
        code: Status.SUCCESS,
        msg: '查询成功',
        content: {
          result: list,
          total
        }
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async findOne(id: string) {
    let resdata: ResData;
    try {
      const result = await this.customFormRepository.findOne(id);
      resdata = {
        code: Status.SUCCESS,
        msg: '查询成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async update(id: string, updateCustomFormDto: UpdateCustomFormDto) {
    let resdata: ResData;
    try {
      const result = await this.customFormRepository.update(id, updateCustomFormDto);
      resdata = {
        code: Status.SUCCESS,
        msg: '更新成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }

  async remove(id: string) {
    let resdata: ResData;
    try {
      const result = await this.customFormRepository.delete(id);
      resdata = {
        code: Status.SUCCESS,
        msg: '删除成功',
        content: result
      };
      return resdata;
    } catch (e) {
      resdata = {
        code: Status.ERROR,
        msg: e,
      };
      return resdata;
    }
  }
}
